package database

import (
	"gin.project.template/config"
	"gin.project.template/global"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"gorm.io/gorm/logger"
	"gorm.io/gorm/schema"
	"time"
)

func InitMySQL() {
	db, err := gorm.Open(mysql.Open(config.Conf.MysqlDns), &gorm.Config{
		Logger: logger.Default.LogMode(logger.Warn),
		NamingStrategy: schema.NamingStrategy{
			TablePrefix:   "prefix_", // 设置表前缀
			SingularTable: true,      // 使用单数表名形式 单数user 复数users
		},
	})
	if err != nil {
		return
	}

	sqlDB, err := db.DB()
	if err != nil {
		return
	}

	sqlDB.SetMaxIdleConns(32)
	sqlDB.SetMaxOpenConns(512)
	sqlDB.SetConnMaxLifetime(time.Hour)
	global.MysqlDB = db
	global.Logger.Info("init mysql success")
}
